package com.zhpan.indicator;

import com.zhpan.indicator.base.BaseIndicatorComponent;
import com.zhpan.indicator.base.MeasureResult;
import com.zhpan.indicator.drawer.DrawerProxy;
import com.zhpan.indicator.enums.IndicatorOrientation;
import com.zhpan.indicator.enums.IndicatorSlideMode;
import com.zhpan.indicator.enums.IndicatorStyle;
import com.zhpan.indicator.options.IndicatorOptions;
import com.zhpan.indicator.utils.AttrValue;
import com.zhpan.indicator.utils.Log;

import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.Optional;

/**
 * 指示器控件
 *
 * @since 2021-04-13
 */
public class IndicatorComponent extends BaseIndicatorComponent implements Component.DrawTask {
    private static final String TAG = IndicatorComponent.class.getSimpleName();

    private int width;
    private int height;

    private DrawerProxy mDrawerProxy;

    /**
     * 创建指示器控件
     *
     * @param context 上下文
     */
    public IndicatorComponent(Context context) {
        super(context);
    }

    /**
     * 创建指示器
     *
     * @param context 上下文
     * @param attrSet 样式参数
     */
    public IndicatorComponent(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init(attrSet);
    }

    /**
     * 创建指示器
     *
     * @param context 上下文
     * @param attrSet 样式参数
     * @param styleName 样式
     */
    public IndicatorComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(attrSet);
    }

    private void init(AttrSet attrSet) {
        mDrawerProxy = new DrawerProxy(mIndicatorOptions);
        int indicatorSlideMode = AttrValue.get(attrSet, "psi_slide_mode", IndicatorSlideMode.NORMAL);
        int indicatorStyle = AttrValue.get(attrSet, "psi_style", IndicatorStyle.CIRCLE);
        Color checkedColor = AttrValue.get(attrSet, "psi_slider_checked_color",
            new Color(Color.getIntColor("#FF4C39")));
        Color normalColor = AttrValue.get(attrSet, "psi_slider_normal_color", new Color(Color.getIntColor("#935656")));
        int orientation = AttrValue.get(attrSet, "psi_orientation", IndicatorOrientation.INDICATOR_HORIZONTAL);
        float radius = AttrValue.get(attrSet, "psi_slider_radius", AttrHelper.vp2px(8, getContext()));

        mIndicatorOptions.setCheckedSliderColor(checkedColor);
        mIndicatorOptions.setNormalSliderColor(normalColor);
        mIndicatorOptions.setOrientation(orientation);
        mIndicatorOptions.setIndicatorStyle(indicatorStyle);
        mIndicatorOptions.setSlideMode(indicatorSlideMode);
        mIndicatorOptions.setSliderWidth(radius * 2, radius * 2);
        setEstimateSizeListener(new EstimateSizeListener() {
            @Override
            public boolean onEstimateSize(int widthSpec, int heightSpec) {
                MeasureResult measureResult = mDrawerProxy.onMeasure(widthSpec, heightSpec);

                width = measureResult.getMeasureWidth();
                height = measureResult.getMeasureHeight();

                setEstimatedSize(width, height);
                Log.d(TAG, "onEstimateSize width=%{public}d, height=%{public}d", IndicatorComponent.this.width, height);
                return false;
            }
        });
        addDrawTask(this);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        setComponentSize(width, height);
        rotateCanvas(canvas);
        mDrawerProxy.onDraw(component, canvas);
    }

    @Override
    public void setIndicatorOptions(IndicatorOptions options) {
        super.setIndicatorOptions(options);
        mDrawerProxy.setOptions(options);
    }

    @Override
    public void notifyDataChanged() {
        mDrawerProxy = new DrawerProxy(mIndicatorOptions);
        super.notifyDataChanged();
    }

    private void rotateCanvas(Canvas canvas) {
        int orientation = mIndicatorOptions.getOrientation();
        int width1 = getWidth();
        if (orientation == IndicatorOrientation.INDICATOR_VERTICAL) {
            canvas.rotate(90f, getWidth() / 2f, width1 / 2f);
        } else if (orientation == IndicatorOrientation.INDICATOR_RTL) {
            canvas.rotate(180f, width1 / 2f, getHeight() / 2f);
        }
    }

    /**
     * 设置方向
     *
     * @param orientation 方向
     */
    public void setOrientation(int orientation) {
        mIndicatorOptions.setOrientation(orientation);
    }
}
